<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <style type="text/css">
        .router-link-active{
            font-size:24px;
        }
    </style>
</head>
<body>
<div id="box">
    <input type="button" value="添加一个路由" @click="push">
    <input type="button" value="替换一个路由" @click="replace">
    <div>
        <router-link to="/home">主页</router-link>
        <router-link to="/user">用户</router-link>
    </div>
    <div>
        <router-view></router-view>
    </div>
</div>
<script src="../../lib/vue.min.js"></script>
<script src="vue-router.min.js"></script>
<script>
//1、组件
var Home={
    template:'<h3>我是主页</h3>'
};
var User={
    template:`
                <div>
                    <h3>我是用户信息</h3>
                    <ul>
                        <li><router-link to="/user/username">某个用户</router-link></li>
                    </ul>
                    <div>
                        <router-view></router-view>
                    </div>
                </div>
            `
};
var UserDetail={
    template:'<div>我是XX用户</div>'
};

//这里把步骤2和步骤3合并一起
const router=new VueRouter({
    routes:[
        {path:'/home', component:Home},
        {
            path:'/user',
            component:User,
            children:[
                {path:'username', component:UserDetail}//核心
            ]
        },
        {path:'*', redirect:'/home'}  //404
    ]
});

//4、最后挂到vue上
new Vue({
    router,
    methods:{
        push(){
            router.push({path:'home'});
        },
        replace(){
            router.replace({path:'user'});
        }
    }
}).$mount("#box");
//router.push({path:'home'});  //直接添加一个路由,表现切换路由，本质往历史记录里面添加一个
//router.replace({path:'news'}) //替换路由，不会往历史记录里面添加
</script>
</body>
</html>